package com.guigu.guliproduct.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.gulicommon.entity.pojo.res.Res;
import com.guigu.guliproduct.entity.Category;
import com.guigu.guliproduct.entity.dto.AddCategoryDto;
import com.guigu.guliproduct.entity.vo.GetCateGoryTreeVo;
import com.guigu.guliproduct.mapper.CategoryMapper;
import com.guigu.guliproduct.service.ICategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.guigu.gulicommon.constant.enumes.product.CatLevel.FIST;
import static com.guigu.gulicommon.constant.enumes.product.ShowDesc.YES;
import static com.guigu.gulicommon.constant.enumes.res.SelectResCode.SELECT_FIRST_CATEGORY_SUCCESS;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author 陈俊杰
 * @since 2024-07-13
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {


    /**
     * 新增商品分类
     *
     * @param category 新增商品分类入参
     * @return 新增商品分类
     */
    @Override
    public Category addCategory(AddCategoryDto category) {
//        Category proCategory = new Category();
//        BeanUtils.copyProperties(category, proCategory);
//
//        //先查询该父分类下是否有相同的分类
//        LambdaQueryWrapper<Category> lambdaQuery = new LambdaQueryWrapper<Category>()
//                .eq(Category::getCatLevel, category.getParentCid())
//                .like(Category::getName, category.getName());
//        if (this.baseMapper.selectCount(lambdaQuery) > 0) {
//            throw new BaseException(AddResCode.INSERT_FAIL_DATA_IS_EXIST);
//        }
//        this.baseMapper.insert(proCategory);
//        return proCategory;
        return null;
    }


    /**
     * 查询所有分类
     *
     * @return 所有一级分类和其子分类
     */
    @Override
    @Cacheable(value = {"category"}, key = "#root.method.name", sync = true)
    public Res<GetCateGoryTreeVo> getAllCategory() {
        //1 查询所有分类
        List<Category> allCategoryTreeVoList = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Category::getShowStatus, YES)
                .list();
        //2 封装分类
        //2.1 过滤一级分类
        List<GetCateGoryTreeVo> cateGoryTreeVos = allCategoryTreeVoList.stream().filter(
                category -> category.getCatLevel().equals(FIST)
        ).map(category -> {
            GetCateGoryTreeVo getCateGoryTreeVo = new GetCateGoryTreeVo();
            BeanUtils.copyProperties(category, getCateGoryTreeVo);
            //2.2 过滤二级分类
            List<GetCateGoryTreeVo> cateGoryTreeVos2 = allCategoryTreeVoList.stream().filter(
                    category2 -> category2.getParentCid().equals(category.getCatId())).map(category2 -> {
                GetCateGoryTreeVo getCateGoryTreeVo2 = new GetCateGoryTreeVo();
                BeanUtils.copyProperties(category2, getCateGoryTreeVo2);
                //2.3 过滤三级分类
                List<GetCateGoryTreeVo> cateGoryTreeVos3 = allCategoryTreeVoList.stream().filter(
                        category3 -> category3.getParentCid().equals(category2.getCatId())).map(category3 -> {
                    GetCateGoryTreeVo getCateGoryTreeVo3 = new GetCateGoryTreeVo();
                    BeanUtils.copyProperties(category3, getCateGoryTreeVo3);
                    return getCateGoryTreeVo3;
                }).collect(Collectors.toList());
                //封装三级分类
                getCateGoryTreeVo2.setChildren(cateGoryTreeVos3);
                return getCateGoryTreeVo2;
            }).sorted(
                    Comparator.comparingInt(GetCateGoryTreeVo::getSort)
            ).collect(Collectors.toList());
            //封装二级分类
            getCateGoryTreeVo.setChildren(cateGoryTreeVos2);
            return getCateGoryTreeVo;
        }).sorted(
                Comparator.comparingInt(GetCateGoryTreeVo::getSort)
        ).collect(Collectors.toList());
        return new Res<>(SELECT_FIRST_CATEGORY_SUCCESS, cateGoryTreeVos);
    }
}
